﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using NPSoft.Ferrero.DataBridge.BLL;
using NPSoft.Ferrero.Framework.AOP;
using NPSoft.Ferrero.Framework.Utility;
using NPSoft.Ferrero.Framework.Web;

namespace NPSoft.Ferrero.DataBridge.Web.Services
{
    /// <summary>
    /// Summary description for FerreroChinaMailService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class FerreroChinaMailService : System.Web.Services.WebService
    {
        public NPSoapHeader soapheader = new NPSoapHeader();

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="identifier">标识符</param>
        /// <param name="key">秘钥</param>
        /// <param name="idTos">CN20/CN30</param>
        /// <param name="errorType">错误类型</param>
        /// <returns>执行结果提示信息</returns>
        [WebMethod]
        [SoapHeader("soapheader")]
        public string SendMail(string key, int identifier, string idTos, int errorType)
        {
            NPMessage result = new NPMessage();
            string str = string.Empty;
            string bodyMsg = string.Empty;
            string src = string.Empty;
            if (soapheader.isValid(key, out str))
            {
                List<string> strTos = new List<string>();
                List<string> strCcs = new List<string>();
                try
                {
                    // 根据CompanyCode值（CN20/CN30）发送给不同邮箱（待确认邮箱）或用于切换UAT和PRD数据库
                    if (idTos == "CN20")
                    {
                        strTos = this.To3.Split(';').ToList();
                        strCcs = this.Cc3.Split(';').ToList();
                    }
                    else if (idTos == "CN30")
                    {
                        strTos = this.To4.Split(';').ToList();
                        strCcs = this.Cc4.Split(';').ToList();
                    }
                    else if (idTos == "MDM")
                    {
                        strTos = this.To.Split(';').ToList();
                        strCcs = this.Cc.Split(';').ToList();
                    }
                    else if (idTos == "DMS")
                    {
                        strTos = this.To2.Split(';').ToList();
                        strCcs = this.Cc2.Split(';').ToList();
                    }

                    IFCR_IN_MailServices svc = NPWebApp.Container.Resolve<IFCR_IN_MailServices>();
                    svc.ID = identifier;
                    svc.ErrorType = errorType;
                    result.Message = svc.SendMail(this.Account, this.Password, strTos, strCcs);
                }
                catch (Exception ex)
                {
                    result.Success = false;
                    result.Message = ex.Message;
                }
            }
            else
            {
                result.Success = false;
                result.Message = str;
            }
            return result.Message;
        }

        /// <summary>
        /// 账号
        /// </summary>
        public string Password
        {
            get
            {
                string encryptPwd = ((NameValueCollection)System.Web.Configuration.WebConfigurationManager.GetSection("NewsPage/mail"))["Password"];
                return EncryptHelper.DesDecrypt(encryptPwd);
            }
        }


        /// <summary>
        /// 账号
        /// </summary>
        public string Account
        {
            get
            {
                string encryptAccount = ((NameValueCollection)System.Web.Configuration.WebConfigurationManager.GetSection("NewsPage/mail"))["Account"];
                return encryptAccount;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string To
        {
            get
            {
                string to = ((NameValueCollection)System.Web.Configuration.WebConfigurationManager.GetSection("NewsPage/mail"))["to"];
                return to;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Cc
        {
            get
            {
                string cc = ((NameValueCollection)System.Web.Configuration.WebConfigurationManager.GetSection("NewsPage/mail"))["cc"];
                return cc;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        public string To2
        {
            get
            {
                string to = ((NameValueCollection)System.Web.Configuration.WebConfigurationManager.GetSection("NewsPage/mail"))["to2"];
                return to;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Cc2
        {
            get
            {
                string cc = ((NameValueCollection)System.Web.Configuration.WebConfigurationManager.GetSection("NewsPage/mail"))["cc2"];
                return cc;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string To3
        {
            get
            {
                string to = ((NameValueCollection)System.Web.Configuration.WebConfigurationManager.GetSection("NewsPage/mail"))["to3"];
                return to;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Cc3
        {
            get
            {
                string cc = ((NameValueCollection)System.Web.Configuration.WebConfigurationManager.GetSection("NewsPage/mail"))["cc3"];
                return cc;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string To4
        {
            get
            {
                string to = ((NameValueCollection)System.Web.Configuration.WebConfigurationManager.GetSection("NewsPage/mail"))["to4"];
                return to;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Cc4
        {
            get
            {
                string cc = ((NameValueCollection)System.Web.Configuration.WebConfigurationManager.GetSection("NewsPage/mail"))["cc4"];
                return cc;
            }
        }
    }
}
